export function extend() {
	var args = arguments;
	var result = args[0] || {};
	for(var i=1; i<args.length; i++) {
		var value = args[i];
		if(value) {
			for(name in value) {
				result[name] = value[name];
			}
		}
	}
	return result;
}

export function extendHtml() {
	var str = null;
	var args = arguments;
	var result = args[0] || {};
	for(var i=1; i<args.length; i++) {
		var value = args[i];
		if(value) {
			for(name in value) {
				str = value[name];
				result[name] = typeof(str) == 'string' ? htmlToText(str) : str;
			}
		}
	}
	return result;
}

const htmlToTextSpan = document.createElement("span");
export const htmlToText = (html) => {
	htmlToTextSpan.innerHTML = html;
	return htmlToTextSpan.innerText;
}

export const each = (items, callback = function(item, index) {}) => {
	for(var i=0; i<items.length; i++) {
		if(callback.call(items[i], items[i], i) == false) {
			break;
		}
	}
}

export const deleteNullValue = (data) => {
	for(name in data) {
		if(data[name] == null) {
			delete data[name];
		}
	}
}

export const upcaseFirstChar = (s) => {
	var first = s.substring(0, 1);
	return first.toUpperCase() + s.substring(1);
}

function S4() {
    return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
}

export const uuid = () => {
    return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
}

export default {
	extend,
	extendHtml,
	htmlToText,
	each,
	uuid,
	deleteNullValue,
	upcaseFirstChar
}